#include <iostream>
#include <thread>
#include <chrono>
#include <functional>
#include "cpp-httplib/httplib.h"

class Download
{
private:
    /* data */
public:
    void download(const std::string & host , const std::string &path ,const std::function<void(const std::string &,const std::string &)> &callback)
    {
        std::cout<<"线程："<<std::this_thread::get_id()<<std::endl;
        httplib::Client client(host);
        auto res=client.Get(path);
        if (res && res->status==200)
        {
            callback(path,res->body);
        }
    };

    void start_download(const std::string & host , const std::string &path ,const std::function<void(const std::string &,const std::string &)> &callback)
    {
        auto download=std::bind(&Download::download,this,std::placeholders::_1,std::placeholders::_2,std::placeholders::_3);
        std::thread thread(download,host,path,callback);
        thread.detach();
    };
};

int main()
{
    auto d= Download();

    auto word_count= [](const std::string &path, const std::string &result)->void{
        std::cout<<"下载完成"<<path << ":" << result.length()<<std::endl;
    } ;

    d.start_download("","",word_count);

    // 休眠10s
    std::this_thread::sleep_for(std::chrono::milliseconds(1000*10));
}
